package cc.wanforme.chipmunity.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cc.wanforme.chipmunity.common.bean.WebResponse;
import cc.wanforme.chipmunity.controller.base.BaseController;
import cc.wanforme.chipmunity.system.po.Subject;
import cc.wanforme.chipmunity.system.service.interfaces.SubjectService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

/** 主题控制器
 * @author wanne
 * 2019年8月17日
 * 
 */
@Controller
@RequestMapping("/subject")
public class SubjectController extends BaseController {
	private static final Logger log = LoggerFactory.getLogger(SubjectController.class);
	
	@Autowired
	private SubjectService subjectService;
	
	@RequestMapping("/add")
	@ResponseBody
	@ApiOperation("添加新主题")
	@ApiImplicitParam(name="subject", dataType="Subject", value="新主题", required=true)
	@PreAuthorize("hasAuthority('subject:add')")
	public WebResponse addSubject(@RequestBody(required=true) Subject subject) {
		try {
			return subjectService.saveSubject(subject);
		} catch (Exception e) {
			log.error("添加主题", e);
			return WebResponse.fail("添加失败！服务器异常！");
		}

	}
	
	@RequestMapping("/update")
	@ResponseBody
	@ApiOperation("更新主题")
	@ApiImplicitParam(name="subject", dataType="Subject", value="根据id更新主题", required=true)
	@PreAuthorize("hasAuthority('subject:update')")
	public WebResponse updateSubject(@RequestBody(required=true) Subject subject) {
		try {
			return subjectService.updateSubject(subject);
		} catch (Exception e) {
			log.error("更新主题", e);
			return WebResponse.fail("更新失败！服务器异常！");
		}
		
	}
	
	@RequestMapping("/delete")
	@ResponseBody
	@ApiOperation("删除主题")
	@ApiImplicitParam(name="subject", dataType="Long", value="删除主题", required=true)
	@PreAuthorize("hasAuthority('subject:delete')")
	public WebResponse deleteSection(@RequestParam(required=true) Long id) {
		try {
			return subjectService.removeSubject(id);
		} catch (Exception e) {
			log.error("删除主题", e);
			return WebResponse.fail("删除失败！服务器异常！");
		}
		
	}
}
